$(document).ready(function(){
	//放大镜
	// 1.抓取元素
	var left = document.getElementById('mediumImg');
	var right = document.getElementById('right');
	var box = document.getElementById('box');
	var img = document.getElementById('img');
	var cover = document.getElementById('cover');
	// 2.鼠标移入
	left.onmousemove=function(e){
		// 右侧和左侧小盒子显示
		right.style.display = "block";
		box.style.display = "block";
		// 获取鼠标事件对象
		var ev = window.event || e ;
		var l = ev.offsetX || ev.layerX;
		var t = ev.offsetY || ev.layerY;
		// 设置小色块的坐标
		var  left = l - 100;
		var  top = t - 100;
		// 范围判断
        if(left>=200){
        	left = 200;
        }
        if(left<=0){
        	left = 0;
        }
        if(top>=200){
        	top = 200;
        }
        if(top<=0){
        	top = 0;
        }
		// 给小色块赋值
		box.style.left = left + "px";
		box.style.top = top + "px";  
		// 给大图片位置赋值(反向2倍)
		img.style.left = left * -2 + "px";   
		img.style.top = top * -2 + "px";
	}
	// 3.鼠标移出
	left.onmouseout=function(){
		// 右侧和左侧小盒子消失
		right.style.display = "none";
		box.style.display = "none";		
	}
	//切换图片显示
	var imgSrc = $("#imageMenu > li > span:first").css("background-image");
	var medium = imgSrc.substr(0,imgSrc.lastIndexOf("_"))+"_medium"+imgSrc.substr(imgSrc.lastIndexOf("."));
	$("#mediumImg").css("background-image",medium);
	$("#mediumImg").mouseover(function(){
		var imgSrc = $(this).css("background-image");
		//alert(imgSrc.lastIndexOf("."));
		var medium = imgSrc.substr(0,imgSrc.lastIndexOf("_"))+"_medium"+imgSrc.substr(imgSrc.lastIndexOf("."));
		var bigimg = imgSrc.substr(5,imgSrc.lastIndexOf("_")-5)+imgSrc.substr(imgSrc.lastIndexOf("."));
		bigimg = bigimg.substr(0,bigimg.length-2);
		$("#mediumImg").css("background-image",medium);
		$("#img").attr("src",bigimg);
	});
	$("#imageMenu > li > span").mouseover(function(){
		var imgSrc = $(this).css("background-image");
		var medium = imgSrc.substr(0,imgSrc.lastIndexOf("_"))+"_medium"+imgSrc.substr(imgSrc.lastIndexOf("."));
		$("#mediumImg").css("background-image",medium);
	});
	//绑定拖动元素对象(carbox)
	bindDrag(document.getElementById('carbox'));
	$("#carbox").css('left',($(window).width()-$("#carbox").width())/2+"px");
	$("#carbox").css('top',($(window).height()-$("#carbox").height())/2+"px");
	$("#carZheZhao").height($(document).height());
	$("#addShoppingCar").click(function(){
		var shopNum = $("#shopNum").val();
		var gid = $("#gid").val();
		$.ajax({
			type:'post',
			url:addCartUrl,
			data:{goodsnum:shopNum,goodsid:gid,param:params},
			dataType:'json',
			success:function(phpData){
				$("#shopcarcount").html(phpData.totalnums);
				$("#shopcarprice").html("￥"+phpData.totalprice);
				$("#carbox").css('left',($(window).width()-$("#carbox").width())/2+"px");
				$("#carbox").css('top',($(window).height()-$("#carbox").height())/2+$(window).scrollTop()+"px");
				$("#carZheZhao").show();
				$("#carbox").show();
			}
		});
		
	});
	$("span.close,#continueshop").click(function(){
		$("#carZheZhao").fadeOut();
		$("#carbox").fadeOut();
	});
});

function bindDrag(el) {
    //初始化参数
    var els = el.style,
    //鼠标的 X 和 Y 轴坐标
	x = y = 0;
    //邪恶的食指
    $(el).mousedown(function (e) {
        var isfixed = checkClassName(document.getElementById("carbox").className);
        document.getElementById("carbox").className = "CarBox_Main CarBox_MainDragDrop";
        //按下元素后，计算当前鼠标位置
        x = e.clientX - el.offsetLeft;
        y = e.clientY - el.offsetTop;
        //IE下捕捉焦点
        el.setCapture && el.setCapture();
        els.top = e.clientY - y + 'px';
        els.left = e.clientX - x + 'px';
        //绑定事件
        $(document).bind('mousemove', mouseMove).bind('mouseup', mouseUp);
    });
    //移动事件
    function mouseMove(e) {
        //宇宙超级无敌运算中...
        els.top = e.clientY - y + 'px';
        els.left = e.clientX - x + 'px';
        //            var top = e.clientY - y + 'px';
        //            var left = e.clientX - x + 'px';
        //            if (isFixed) {
        //                $(".CarBox_MainFixed").css("top", top);
        //                $(".CarBox_MainFixed").css("left", left);
        //            }
        //            else {
        //                $(".CarBox_Main").css("top", top);
        //                $(".CarBox_Main").css("left", left);
        //            }
        ////            els.css("top", e.clientY - y + 'px');
        ////            els.css("left", e.clientX - x + 'px');
    }
    //停止事件
    function mouseUp() {
        isFixed = false;
        //IE下释放焦点
        el.releaseCapture && el.releaseCapture();
        //卸载事件
        $(document).unbind('mousemove', mouseMove).unbind('mouseup', mouseUp);
    }
    function checkClassName(name) {
        var nameArr = name.split(' ');
        for (var i = 0; i < nameArr.length; i++) {
            if (nameArr[i] == "CarBox_MainFixed") {
                return true;
            }
        }
        return false;
    }
}
